package com.tgu.home_action.service.impl;

import com.tgu.home_action.dao.CaseMapper;
import com.tgu.home_action.dao.ClientMapper;
import com.tgu.home_action.dao.OldManMapper;
import com.tgu.home_action.pojo.*;
import com.tgu.home_action.pojo.state.RedisHeader;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.ThirdPartyService;
import com.tgu.home_action.service.utils.JwtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class ThirdPartyServiceImpl implements ThirdPartyService {
    @Autowired
    private CaseMapper caseMapper;
    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private JwtService jwtService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private OldManMapper oldManMapper;

    /**
     * 404接口
     */
    @Override
    public ResponseData<List<Case>> notFound(Integer count) {
        List<Case> cases = caseMapper.getRandCases(count);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), cases);
    }


    /**
     * 登录
     */
    @Override
    public ResponseData<Client> login(Client client) {
        Client rightClient = clientMapper.getByAppId(client.getAppId());
        if (rightClient == null) {
            return new ResponseData<>(ResponseState.USER_NOT_EXIST.getMessage(), ResponseState.USER_NOT_EXIST.getValue());
        }
        if (!rightClient.getAccessKey().equals(client.getAccessKey())) {
            return new ResponseData<>(ResponseState.APP_KEY_ERROR.getMessage(), ResponseState.APP_KEY_ERROR.getValue());
        }
        String token = jwtService.getAccessToken(rightClient.getId(), "client");
        String refreshToken = jwtService.getRefreshToken(rightClient.getId(), "client");
        stringRedisTemplate.opsForValue().set(RedisHeader.CLIENT_ACCESS_TOKEN.getHeader() + rightClient.getId(), token, 2, TimeUnit.HOURS);
        stringRedisTemplate.opsForValue().set(RedisHeader.CLIENT_REFRESH_TOKEN.getHeader() + rightClient.getId(), refreshToken, 15, TimeUnit.DAYS);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), token, refreshToken, rightClient);

    }

    /**
     * 更新Token
     */
    @Override
    public ResponseData<Object> refreshToken(String refreshToken) {
        Client client = (Client) jwtService.getUser(refreshToken);
        if (client == null) {
            return new ResponseData<>(ResponseState.REFRESH_TOKEN_IS_ERROR.getMessage(), ResponseState.REFRESH_TOKEN_IS_EXPIRED.getValue());
        }
        String rightToken = stringRedisTemplate.opsForValue().get(RedisHeader.CLIENT_REFRESH_TOKEN.getHeader() + client.getId());
        if (!refreshToken.equals(rightToken)) {
            return new ResponseData<>(ResponseState.REFRESH_TOKEN_IS_ERROR.getMessage(), ResponseState.REFRESH_TOKEN_IS_ERROR.getValue());
        }
        String newAccessToken = jwtService.getAccessToken(client.getId(), "client");
        String newRefreshToken = jwtService.getRefreshToken(client.getId(), "client");
        stringRedisTemplate.opsForValue().set(RedisHeader.CLIENT_ACCESS_TOKEN.getHeader() + client.getId(), newAccessToken, 2, TimeUnit.HOURS);
        stringRedisTemplate.opsForValue().set(RedisHeader.CLIENT_REFRESH_TOKEN.getHeader() + client.getId(), newRefreshToken, 15, TimeUnit.DAYS);
        return new ResponseData<Object>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), newAccessToken, newRefreshToken);

    }

    /**
     * 查询个人信息
     */
    @Override
    public ResponseData<Client> getClient(Client client) {
        System.out.println("====================================");
        client = clientMapper.getById(client.getId());
        System.out.println(client);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), client);
    }

    /**
     * 通过ID查询案件
     */
    @Override
    public ResponseData<Case> getCaseById(Integer caseId) {
        Case nowCase = caseMapper.getById(caseId);
        nowCase.setOldMan(oldManMapper.getById(nowCase.getOldManId()));
        System.out.println(nowCase.getOldManId());
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), nowCase);
    }

    /**
     * 查询所有案件信息
     */
    @Override
    public ResponseData<Page<Case>> getCases(Case nowCase) {
        Client rightClient = clientMapper.getByAppId(nowCase.getAppId());
        if (rightClient == null) {
            return new ResponseData<>(ResponseState.USER_NOT_EXIST.getMessage(), ResponseState.USER_NOT_EXIST.getValue());
        }
        if (!rightClient.getAccessKey().equals(nowCase.getAccessKey())) {
            return new ResponseData<>(ResponseState.APP_KEY_ERROR.getMessage(), ResponseState.APP_KEY_ERROR.getValue());
        }
        int sum = caseMapper.getAccordCaseNumber(nowCase);
        if (nowCase.getPageNo() == null || nowCase.getPageSize() == null) {
            nowCase.setPageNo(1);
            nowCase.setPageSize(sum);
        }
        nowCase.setStartSize((nowCase.getPageNo() - 1) * nowCase.getPageSize());
        List<Case> cases = caseMapper.getAccordCase(nowCase);
        int pageNum;
        if (nowCase.getPageSize() == 0) {
            pageNum = 0;
        } else {
            pageNum = sum / nowCase.getPageSize();
            if (sum % nowCase.getPageSize() > 0) {
                pageNum++;
            }
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), new Page<>(nowCase.getPageNo(), nowCase.getPageSize(), pageNum, sum, cases));

    }
}
